package cn.yikaojiuguo.student.ui.ask;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Random;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.adapter.AskAdapter;
import cn.yikaojiuguo.student.app.YiKaoApplication;
import cn.yikaojiuguo.student.support.simple.ArgsConstants;
import cn.yikaojiuguo.student.support.utils.MyImgLoader;
import cn.yikaojiuguo.student.support.utils.ToastUtil;
import cn.yikaojiuguo.student.support.utils.Util;
import cn.yikaojiuguo.student.ui.base.BaseImgsActivity;
import cn.yikaojiuguo.student.ui.base.CustomerActionBarActivity;
import cn.yikaojiuguo.student.ui.global.AblumActivity;

public class AskActivity extends CustomerActionBarActivity implements View.OnClickListener {

    @Bind(R.id.gv)
    GridView mGv;
    private AskAdapter mAdapter;
    @Bind(R.id.ll_record)
    LinearLayout mLl_record;
    @Bind(R.id.tv_time)
    TextView mTv_time;
    @Bind(R.id.ll_pic)
    LinearLayout mLl_pic;
    @Bind(R.id.iv_wave)
    ImageView mIv_wave;
    @Bind(R.id.et_ask)
    EditText mEt_ask;

    public final static int CHOOSE_PIC = 1;
    public final static int TAKE_PHOTO = 2;
    public final static int RECORD = 3;
    public final static int LOOK_BIG = 4;
    private AlertDialog mDialog;
    private String camera_file_path;
    private String mFixed_time;
    private String mLong_time;
    private int[] mWavs_id = {R.drawable.record_wave1, R.drawable.record_wave2, R.drawable
            .record_wave3, R.drawable.record_wave4, R.drawable.record_wave5, R.drawable
            .record_wave6, R.drawable.record_wave7, R.drawable.record_wave8, R.drawable
            .record_wave9, R.drawable.record_wave10,};

    private int mType = CHOOSE_PIC;
    private ArrayList<String> mPicsPath = new ArrayList<String>();
    private String mContent;

    public static Intent newIntent(int type) {
        Intent intent = new Intent(YiKaoApplication.getInstance(), AskActivity.class);
        intent.putExtra(ArgsConstants.TYPE, type);
        return intent;
    }

    public static Intent newRecordIntent(String fixedTime, String longtime) {
        Intent intent = new Intent(YiKaoApplication.getInstance(), AskActivity.class);
        intent.putExtra(ArgsConstants.TYPE, RECORD);
        intent.putExtra(ArgsConstants.FIXED_TIME, fixedTime);
        intent.putExtra(ArgsConstants.LONG_TIME, longtime);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ask);
        ButterKnife.bind(this);
        switch (getIntent().getIntExtra(ArgsConstants.TYPE, 0)) {
            case CHOOSE_PIC:
                mType = CHOOSE_PIC;
                startActivityForResult(AblumActivity.newIntent(3, mPicsPath), CHOOSE_PIC);
                break;
            case TAKE_PHOTO:
                mType = TAKE_PHOTO;
                camera_file_path = Util.takePhoto(this, TAKE_PHOTO);
                break;
            case RECORD:
                mType = RECORD;
                mFixed_time = getIntent().getStringExtra(ArgsConstants.FIXED_TIME);
                mLong_time = getIntent().getStringExtra(ArgsConstants.LONG_TIME);
                break;
        }
        initView();
        initEvents();
    }

    private void initEvents() {
        mGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == mPicsPath.size()) {
                    if (position <= mAdapter.MAX_NUM - 1) {
                        showDialog();
                    } else {
                        startActivityForResult(BaseImgsActivity.newIntent(AskImgsActivity.class,
                                mPicsPath,
                                position, true), LOOK_BIG);
                    }
                } else {
                    startActivityForResult(BaseImgsActivity.newIntent(AskImgsActivity.class,
                            mPicsPath, position, true), LOOK_BIG);
                }
            }
        });
        setCustomerOperationOnClickListener(this);
    }

    private void initView() {
        setCustomerTitle("");
        setCustomerOperationText("确定");
        setCustomerBackText("取消");
        if (mType == RECORD) {
            mLl_pic.setVisibility(View.GONE);
            mLl_record.setVisibility(View.VISIBLE);
            mTv_time.setText(mFixed_time);
            Random r = new Random();
            int i = r.nextInt(10);
            mIv_wave.setImageResource(mWavs_id[i]);
        } else {
            mLl_record.setVisibility(View.GONE);
            mLl_pic.setVisibility(View.VISIBLE);
            mAdapter = new AskAdapter(this, mPicsPath);
            mGv.setAdapter(mAdapter);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case CHOOSE_PIC:
                if (resultCode == Activity.RESULT_OK) {
                    //data中返回的list与askadpter中的list并非一个对象
                    mPicsPath.clear();
                    mPicsPath.addAll(data.getStringArrayListExtra(ArgsConstants.PICS));
                    mAdapter.notifyDataSetChanged();
                }
                break;
            case TAKE_PHOTO:
                if (resultCode == Activity.RESULT_OK) {
                    mPicsPath.add(camera_file_path);
                    mAdapter.notifyDataSetChanged();
                }
                break;
            case LOOK_BIG:
                if (resultCode == Activity.RESULT_OK) {
                    mPicsPath.clear();
                    mPicsPath.addAll(data.getStringArrayListExtra(ArgsConstants.PICS));
                    mAdapter.notifyDataSetChanged();
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyImgLoader.getInstance(3, MyImgLoader.Type.FILO).clearCache();
    }

    public void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog
                .THEME_DEVICE_DEFAULT_LIGHT);
        View contentView = View.inflate(this, R.layout.layout_dialog_choose_pic, null);
        contentView.findViewById(R.id.tv_take).setOnClickListener(this);
        contentView.findViewById(R.id.tv_choose).setOnClickListener(this);
        mDialog = builder.create();
        mDialog.show();
        Window window = mDialog.getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setContentView(contentView);
        window.setLayout((int) (Util.getScreenWidthPixels(this) * 0.8), ViewGroup
                .LayoutParams.WRAP_CONTENT);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_take:
                camera_file_path = Util.takePhoto(this, TAKE_PHOTO);
                mDialog.dismiss();
                break;
            case R.id.tv_choose:
                startActivityForResult(AblumActivity.newIntent(3, mPicsPath), CHOOSE_PIC);
                mDialog.dismiss();
                break;
            case R.id.tv_operation:
                if(checkData()) {
                    if(mPicsPath!=null && mPicsPath.size()!=0) {
                        startActivity(AskChooseSecondMajorActivity.newPicsIntent(mPicsPath,mContent));
                    } else if(mType == RECORD) {
                        startActivity(AskChooseSecondMajorActivity.newVoiceIntent(mContent,mLong_time));
                    } else {
                        startActivity(AskChooseSecondMajorActivity.newIntent(mContent));
                    }
                }
                break;
        }
    }

    private boolean checkData() {
        mContent = mEt_ask.getText().toString().trim();
        if(TextUtils.isEmpty(mContent)) {
            ToastUtil.showToast(this,"请输入问答内容");
            return false;
        }
        return true;
    }

}
